package Student_information.Service;

import Student_information.Javabean.Student;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StudentService {
    public void findAll(List<Student> list) {
        if (list.size() == 0) {
            System.out.println("目前没有学生信息");
            return;

        } else {
            System.out.println("学号\t姓名\t性别\t年龄");
            for (Student s : list) {
                System.out.println(s.getStuno() + "\t" + s.getName() + "\t" + s.getSex() + "\t" + s.getAge());

            }
        }

    }

    public void addStu(List<Student> list) {
        //获取用户输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String stuno = sc.nextLine();
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入性别");
        String sex = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        Student s = new Student(stuno, name, sex, age);
        list.add(s);
        findAll(list);
    }

    public void deleteStu(List<Student> list) {

        //获取需要删除的学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String stuno = sc.nextLine();
        boolean flag = false;

        //创建迭代器
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            if (stuno.equals(s.getStuno())) {
                it.remove();
                flag = true;
            }
        }
        if (flag) {
            System.out.println("删除成功");
            findAll(list);
        } else {
            System.out.println("删除失败");
        }
    }

    public void updateStu(List<Student> list) {
        //获取需要修改的学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号");
        String stuno = sc.nextLine();
        boolean flag = false;
        //遍历集合
        for (Student s : list) {
            if (stuno.equals(s.getStuno())) {
                System.out.println("请重新输入姓名");
                String name = sc.nextLine();
                System.out.println("请重新输入性别");
                String sex = sc.nextLine();
                System.out.println("请重新输入年龄");
                String age = sc.nextLine();
                s.setName(name);
                s.setStuno(stuno);
                s.setSex(sex);
                s.setAge(age);
                flag = true;
            }
        }
        if (flag) {
            System.out.println("修改成功");
            findAll(list);
        } else {
            System.out.println("修改失败");
        }
    }
}
